/*
 * Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
 */
package com.pitop.setting.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.SettingException;
import com.pitop.setting.api.SettingFeignApi;
import com.pitop.setting.entity.Setting;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 设置
 *
 * @author pi-top
 */
@Service("settingSVImpl")
@Slf4j
public class SettingSVImpl {

    @Autowired
    private SettingFeignApi settingFeignApi;

    /**
     * 加载一个对象Setting
     *
     * @param id id
     * @param k  键
     * @return Setting
     */
    public Setting load(Long id, String k) {
        if (id == null && k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return settingFeignApi.load(id, k);
    }

    /**
     * 加载一个对象Setting 通过id
     *
     * @param id id
     * @return Setting
     */
    public Setting loadById(Long id) {
        if (id == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return settingFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Setting 通过k
     *
     * @param k 键
     * @return Setting
     */
    public Setting loadByK(String k) {
        if (k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return settingFeignApi.loadByK(k);
    }


    /**
     * 删除对象Setting
     *
     * @param id id* @param k 键
     * @return Setting
     */

    public void delete(Long id, String k) {
        if (id == null && k == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingFeignApi.delete(id, k);
    }


    /**
     * 查询Setting分页
     *
     * @param setting  对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Setting>
     */
    public List<Setting> list(Setting setting, int curPage, int pageSize) {
        return settingFeignApi.list(setting, curPage, pageSize);
    }

    /**
     * 查询Setting分页
     *
     * @param id       id* @param k  键
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Setting>
     */
    public List<Setting> list(Long id, String k, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Setting setting = new Setting();
        setting.setId(id);
        setting.setK(k);

        return settingFeignApi.list(setting, curPage, pageSize);
    }


    public int count(Long id, String k) {
        return settingFeignApi.count(id, k);
    }


    public int count(Setting setting) {
        return settingFeignApi.count(setting);
    }


    /**
     * 保存
     *
     * @param setting 实体
     * @throws BaseException
     */
    public void save(Setting setting) {
        if (setting == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingFeignApi.build(setting);
    }

    /**
     * 更新
     *
     * @param setting 实体
     * @throws BaseException
     */
    public void modify(Setting setting) {
        if (setting == null) {
            throw new SettingException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        settingFeignApi.modify(setting);
    }

}
